package com.zx.JDK特性.jdk1_9.http;

import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandler;
import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.Charset;
import java.time.Duration;

public class HttpDemo {

    public static void main(String[] args) throws Exception {
        HttpClient client = HttpClient.newHttpClient();

        HttpRequest getRequest = HttpRequest
                .newBuilder(new URI("http://www.zx1989.top/"))
                .timeout(Duration.ofSeconds(2))
                .setHeader("User-Agent",
                        "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/76.0.3809.100 Safari/537.36")
                .GET()
                .build();

        BodyHandler<String> bodyHandler = BodyHandlers.ofString(Charset.forName("UTF-8"));

        HttpResponse<String> response = client.send(getRequest, bodyHandler);

        String html = response.body();

        System.out.println(html);
    }

}
